package pl.umk.mat.fred.unicore.urc.storagemanagement.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import pl.umk.mat.fred.unicore.urc.storagemanagement.StorageManagementActivator;
import pl.umk.mat.fred.unicore.urc.storagemanagement.views.TimeMeterView;

public class StopTimeMeterThreadAction extends Action{

	private TimeMeterView view = null;
	
	public StopTimeMeterThreadAction(TimeMeterView view) {
		this.view = view;
		this.setText("Stop Time Meter");
		this.setToolTipText("Stop Time Meter");
		this.setImageDescriptor(getImage());
		this.checkEnabled();
	}
	
	@Override
	public void run() {
		StorageManagementActivator.getDefault().getTimeMeter().setWorking(false);
		setImageDescriptor(getImage());
		checkEnabled();
		view.getRunTimeMeterThreadAction().refreshState();
	}
	
	
	private void checkEnabled() {
		if (StorageManagementActivator.getDefault().isTimeMeterWorking()) {
			setEnabled(true);
		} else {
			setEnabled(false);
		}
	}
	
	private ImageDescriptor getImage() {
		if (StorageManagementActivator.getDefault().isTimeMeterWorking()) {
			return PlatformUI.getWorkbench().getSharedImages().
					getImageDescriptor(ISharedImages.IMG_ELCL_STOP);
		} else {
			return PlatformUI.getWorkbench().getSharedImages().
				getImageDescriptor(ISharedImages.IMG_ELCL_STOP_DISABLED);
		}
	}
	
	public void refreshState() {
		setImageDescriptor(getImage());
		checkEnabled();
	}
}
